import UnityEngine

class PlayerControls (MonoBehaviour): 
    public cursor as (Texture2D)
    
    movement as Movement
    
    def OnEnable ():
        Screen.showCursor = false
        movement = GetComponent(Movement)
    
    def Update():
        movement.planar = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"))
        movement.turn = Input.GetAxis("Turn")
        # networkView.RPC("UpdateMovement", RPCMode.All, Vector3(movement.planar.x, movement.planar.y, movement.turn))
        
        if Input.GetButtonDown("Fire3"):
            mouse = Input.mousePosition
            ray = Camera.main.ScreenPointToRay(mouse)
            if Network.isClient:
                networkView.RPC("Fire", RPCMode.Server, ray.origin, ray.direction)
            else:
                Fire(ray.origin, ray.direction)
            # SendMessage("AutoFire")
        
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward))
        
        if Input.GetButtonDown("Fire1"):
            GetComponent(Health).health = -1
            # SendMessage("OnDeath")
            # Network.Destroy(gameObject)
    
    [RPC]
    def Fire(origin as Vector3, direction as Vector3):
        GetComponent(Gun).AutoFire(origin, direction)
    
    def OnGUI():
        hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition))
        hit as bool = false
        for h in hits:
            if h.collider.GetComponent(HitBox):
                hit = true
                break
        if hit: cursori = 1
        else: cursori = 0
        GUI.DrawTexture(
            Rect(Input.mousePosition.x-25,Screen.height-Input.mousePosition.y-25,50,50), 
            cursor[cursori]
        )
    
